{{define "permissions.list"}}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="/statics/third-pkg/layui/css/layui.css">
    <link rel="stylesheet" href="/statics/admin/css/public.css" media="all">
    <link rel="stylesheet" href="/statics/third-pkg/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>


<body class="layui-hide">
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-normal" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-btn-group">
                    <button class="layui-btn layui-btn-normal layui-btn data-add-btn" id="add"> 添加</button>
                    <button class="layui-btn" id="btn-expand">全部展开</button>
                    <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                </div>
                <table id="power-table" lay-filter="power-table"></table>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
</script>
<!-- 表格操作列 -->
</body>
<script src="/statics/third-pkg/layui/layui.js"></script>
<script src="/statics/admin/js/lay-config.js?v=2.0" charset="utf-8"></script>
<script>
    layui.use(['table',"treetable",'table','form','request'], function() {
        var $ = layui.jquery;
        var form=layui.form;
        var treetable = layui.treetable;
        var request=layui.request;
        var table=layui.table;
        $('body').removeClass('layui-hide');

        treetable.render({
            treeColIndex: 1,
            treeSpid: null,
            id:"power-table",
            treeIdName: 'id',
            treePidName: 'parent_id',
            skin:'line',
            treeDefaultClose: true,
            elem: '#power-table',
            url: '/admin/permissions/list-data',
            page: false,
            cols: [
                [
                    {type: 'numbers'},
                    {field: 'display_name', minWidth: 200, title: '权限名称'},
                    {field: 'code', minWidth: 200, title: '权限标识'},
                    {field: 'icon', title: '图标',templet: function (d) {
                            return ` <i class="fa ${d.icon}"></i>`
                        }},
                    {field: 'sort', title: '排序'},
                    {field: 'is_menu', width: 80, align: 'center', templet: function (d) {
                            if (d.is_menu == 1) {
                                return '<span class="layui-badge layui-bg-gray">按钮</span>';
                            }
                            if (d.parent_id == null) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            } else {
                                return '<span class="layui-badge-rim">菜单</span>';
                            }
                        }, title: '权限类型'
                    },
                    {title: '操作',templet: '#currentTableBar', width: 150, align: 'center'}
                ]
            ]
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var name = data.field.name;
            treetable.search('#power-table',name);
            return false;
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#power-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#power-table');
        });
        window.addEventListener("storage", function (e) {
            var item_add = localStorage.getItem("permissions-add");
            if(item_add){
                treetable.reload("#power-table");
            }

        });
        $('#add').click(function (){
            var index = layer.open({
                title: '添加菜单',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/admin/permissions/add',
                success: function (lay, index) {
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        })
        //监听工具条
        table.on('tool(power-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'delete') {
                layer.confirm('确定删除当前权限吗', function (index) {
                    request.delete("/admin/permissions/del?id="+data.id).then((res)=>{
                        if(res.status==204){
                            layer.msg("删除权限成功",{
                                time:1000
                            },function (){
                                obj.del();
                            })
                        }else{
                            layer.msg(res.data.msg)
                        }
                    }).catch((err)=>{
                        layer.msg(err)
                    })

                    layer.close(index);
                });
            } else if (layEvent === 'edit') {
                var editIndex = layer.open({
                    title: '编辑权限',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/permissions/edit?id=' + data.id,
                });
            }
        });
    });


</script>
</html>
{{ end }}